package tools;

import bean.LogBean;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 *日志模拟程序
 */
public class GenerData {
    public static void main(String[] args) throws Exception {
        Random r = new Random();
        long ts = new Date().getTime();
        long[] datesLong = getDateLong(15);
        int tidBase = 50;
        int oidBase = 10;
        int timeBase = 24*60*60*1000;
        int successBase = 2;
        String[] modules = getModules();
        BufferedWriter bw = new BufferedWriter(new FileWriter("d:/log0810.txt"));
        for(int i=0;i<15*1000000;i++){
            LogBean b = new LogBean();
            b.setTenantId(r.nextInt(tidBase));
            b.setOperatorId(r.nextInt(oidBase));
            b.setCreateTime(datesLong[r.nextInt(datesLong.length)] - r.nextInt(timeBase));
            b.setSuccess(r.nextInt(successBase));
            b.setModuleFunction("登入系统");
            b.setModular(modules[r.nextInt(modules.length)]);
            bw.write(b.format());
        }
        bw.flush();
        bw.close();
    }

    public static long[] getDateLong(int days) throws ParseException {
        String[] dates = getDate(days);
        long[] dateL = new long[dates.length];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<dates.length;i++){
            dateL[i] = sdf.parse(dates[i]).getTime();
        }
        return dateL;
    }

    public static String[] getDate(int days){
        String[] day = new String[days];
        for(int i=0;i<days;i++){
            day[i]= getDay(-i);
        }
        return day;
    }

    public static String getDay(int i){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,i);
        String day = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return day;
    }

    public static String[] getModules(){
        String modules = "用户管理,资产可视化,我的服务,云监控,我的应用,我的工厂,我的看板,资产结构管理";
        return modules.split(",");
    }
}
